package com.own.business.resource.business.service.impl;

import com.own.business.resource.business.form.SignatureCommonForm;
import com.own.business.resource.business.service.FileRecordService;
import com.own.business.resource.business.service.ResourceService;
import com.own.business.resource.business.util.FileRecordUtil;
import com.own.business.resource.common.model.UploadModelConfig;
import com.own.business.resource.core.ResolveObjectStoreUtil;
import com.own.business.resource.core.em.ResourceGlobalUniqueEnum;
import com.own.business.resource.core.em.ResourceUniqueEnum;
import com.own.business.resource.core.model.SignatureModel;
import com.own.business.resource.core.model.UploadFileNameModel;
import com.own.business.resource.core.model.UploadModel;
import com.own.business.resource.core.property.ResourceProperty;
import com.own.business.resource.entity.vo.FileRecordVo;
import com.own.component.store.core.Store;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * ResourceServiceImpl
 *
 * @author chenxueli
 * @date 2023-07-07 14:28:53
 */
@Slf4j
@Service
public class ResourceServiceImpl implements ResourceService {

    @Resource
    private FileRecordService fileRecordService;
    @Resource
    private ResourceProperty resourceProperty;

    @Resource
    private Store<String> store;

    /**
     * 获取签名信息
     *
     * @param method   上传方式
     * @param fileName 文件名称
     * @param form     其他信息
     * @return 签名信息
     */
    @Override
    public SignatureModel signature(String method, String fileName, SignatureCommonForm form) {
        var objectStoreFile = new UploadFileNameModel(fileName);
        var storeUtil = ResolveObjectStoreUtil.get(method);
        if (form != null) {
            var position = storeUtil.getProperties().getPosition();
            if (StringUtils.isNotBlank(form.getMd5()) && StringUtils.isNotBlank(position)) {
                // 去查询有没有对应的md5的文件
                var fileRecord = fileRecordService.getByMd5(form.getMd5(), position);
                if (fileRecord != null) {
                    return SignatureModel.builder().fileId(fileRecord.getId()).build();
                }
            }
            // 设置文件路径
            if (StringUtils.isNotBlank(form.getFolder())) {
                objectStoreFile.setFolder(form.getFolder());
            }
            objectStoreFile.appendDateFolder();
            // 设置文件名称
            if (form.getRandomName() == null || form.getRandomName()) {
                objectStoreFile.randomName();
            }
        } else {
            objectStoreFile.setDateFolder();
            objectStoreFile.randomName();
        }
        return storeUtil.signature(objectStoreFile.getFolder("/") + "/" + objectStoreFile.getFileName());
    }

    /**
     * 上传文件
     *
     * @param method 上传方式
     * @param file   上传的文件
     * @param config 上传配置
     * @return 签名信息
     */
    @Override
    public FileRecordVo upload(String method, UploadModel file, UploadModelConfig config) {
        // 按照全局的配置计算文件唯一性
        if (ResourceGlobalUniqueEnum.UNIQUE.equals(resourceProperty.getUnique())) {
            var list = fileRecordService.getByMd5(file.getMd5());
            // 如果当前文件列表不为空，则返回第一个数据
            if (!list.isEmpty()) {
                return list.getFirst();
            }
        }
        // 获取文件上传工具
        var util = ResolveObjectStoreUtil.get(method);
        // 按照子模块的配置计算文件唯一性
        if (ResourceGlobalUniqueEnum.NEXT.equals(resourceProperty.getUnique()) && ResourceUniqueEnum.UNIQUE.equals(util.getProperties().getUnique())) {
            var list = fileRecordService.listByMd5(file.getMd5(), util.position());
            // 如果当前文件列表不为空，则返回第一个数据
            if (!list.isEmpty()) {
                return list.getFirst();
            }
        }
        file.setPosition(util.getProperties().getPosition());
        // 设置日期文件夹
        file.setDateFolder();
        // 文件重命名
        if (config.getIsRandomName()) {
            file.randomName();
        }
        // 上传文件
        util.uploadFile(file);
        // 存入数据库
        return fileRecordService.afterUpload(FileRecordUtil.build(file));
    }
}
